	jQuery(document).ready(function() {
	/*Hide Div*/
	jQuery("#validity, #choosePromo, #commitment, #promoConfirm").hide();
	var current_pos = getCookie("fb_current_pos");
	if (current_pos > 1) {
		//console.log(current_pos + "TRUE");
		if(current_pos=="2") {
		jQuery("div.wizard a:nth-of-type(1)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(2)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		}
		if(current_pos=="3") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(3)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
		
		  var mode = getCookie("fb_mode");
		  var promo = getCookie("fb_promo");
		  var commitmentBox = getCookie("fb_commitmentBox");
		  
          if(mode!="edit"){      
			jQuery(".promoBox, .commitmentBox").addClass("active");
			jQuery('#pname, #pemail, #pdob, #padd').hide();
			jQuery('#promo').html(promo);
			jQuery('#commitmentBox').text(commitmentBox);
		  }
		}
		if(current_pos=="4") {
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(4)").attr("class","current");
		jQuery("div.wizard a:nth-of-type(5)").removeAttr("href");
		
		  var mode = getCookie("fb_mode");
		  var promo = getCookie("fb_promo");
		  var commitmentBox = getCookie("fb_commitmentBox");
		  var mobtel = getCookie("fb_mobtel");
		  
          if(mode!="edit"){      
			jQuery(".promoBox, .commitmentBox, .prepaidBox").addClass("active");
			jQuery('#pname, #pemail, #pdob, #padd').hide();
			jQuery('#promo').html(promo);
			jQuery('#commitmentBox').text(commitmentBox);
			jQuery('#mobtel').text(mobtel);
		  }
		}
		if(current_pos=="5") {
		jQuery("div.wizard a:nth-of-type(1),div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3),div.wizard a:nth-of-type(4)").attr("class","current done");
		jQuery("div.wizard a:nth-of-type(5)").attr("class","width100 current");
		
		  var mode = getCookie("fb_mode");;
		  var promo = getCookie("fb_promo");
		  var commitmentBox = getCookie("fb_commitmentBox");
		  var mobtel = getCookie("fb_mobtel");
		  var username = getCookie("fb_username");
		  var eadd = getCookie("fb_eadd");
		  var bday = getCookie("fb_bday");
		  var address = getCookie("fb_address");
		  
          if(mode!="edit"){ 
			jQuery(".promoBox, .commitmentBox, .prepaidBox, .profileBox").addClass("active");
			jQuery(".pnone").hide();
			jQuery('#promo').html(promo);
			jQuery('#commitmentBox').text(commitmentBox);
			jQuery('#mobtel').text(mobtel);
			jQuery('#name').text(username);
			jQuery('#email').text(eadd);
			jQuery('#dob').text(bday);
			jQuery('#add').text(address);
			jQuery("#pname, #pemail, #pdob, #padd").show();
		  }
		}
	} else {
		//console.log(current_pos + "FALSE");
		jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2), div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
	} 
	
/* . . . . . . . . . . . . . . . Ajax Promo . . . . . . . . . . . . . . . . . . */

	var base_url = baseurl + "promo/";
	
	if (/promo/.test(self.location.href)){
		var gadget_id = getCookie("fb_sku");
		jQuery.getJSON(base_url+"getGadgetId/"+gadget_id,{ajax:'true'},function(j){
		   for(var i=0;i<j.length;i++){
			   document.cookie="fb_gadgetid=" + j[i].gadgetId + "; path=/";
		   }
		});
	
		jQuery.getJSON(base_url+"getpromotypelist",{ajax:'true'},function(j){
		var ht = '';
		//j.reverse(sort_by('promoTypeId', true, parseInt));
		j.sort(sort_by('promoTypeId', true, parseInt));
		for(var i=0;i<j.length;i++){
           //var codeLine = j[i].promoTypeName;
	       //var firstWord = codeLine.substr(0, codeLine.indexOf(" "));
           ht+='<a href="#." class="selectPromo inBlock"><span style="display:none;" id="promoTypeid" >'+j[i].promoTypeId+'</span><span class="promoIcons1 floatLeft"></span><p class="floatLeft font12indfb">With '+j[i].promoTypeDescription+'</p><h5 class="clear marg0 font24imprev">'+j[i].promoTypeName+'</h5></a>';
		}
			ht+='<div class="clear"></div>';
			jQuery("#promoType-inner").html(ht);
			jQuery("div#promoType-inner a:nth-of-type(2) span#img_icon").attr("class","promoIcons2 floatLeft");		
		});
    }
	
	/*Choose Promo Type*/
	jQuery('#promoType-inner').on("click", "a", function () {
		var $this = $(this);
	   	$this.siblings().removeClass('active');
	   	if ($this.hasClass('active')) {
	      	$this.removeClass('active');
			jQuery("#validity, #choosePromo, #commitment, #promoConfirm").hide();
			jQuery("#commitment a, #choosePromo a, #validity a, .promoBox, .commitmentBox").removeClass('active');
			jQuery("p#promo, p#commitmentBox").html("None")
			return false;
	   	} else {
	      	$this.addClass('active');
			jQuery("#validity, #choosePromo, #commitment, #promoConfirm").hide();
			jQuery("#validity a, #choosePromo a, #commitment a").removeClass('active');
			jQuery("p#promo, p#commitmentBox").html("None")
			jQuery(".promoBox, .commitmentBox").removeClass('active');
			var promo_id = jQuery("#promoType-inner .active").children('span').html();

			jQuery.getJSON(base_url+"getpromovaliditylist/"+promo_id,{ajax:'true'},function(j){
				j.sort(sort_by('validity', true, parseInt));
				var ht = '';
				for(var i=0;i<j.length;i++){
				   if(j[i].validity=='1') day='day'; else day='days';
				   ht+='<a href="#." class="selectValidity inBlock"><h5 id="numofdays">'+j[i].validity+'</h5><p class="posRel topnega10">'+day+'</p></a>';
				}
				jQuery("div#validity").show();
				jQuery("#validity-inner").html(ht);
				FB.Canvas.scrollTo(0,400);
			});
		}
	});
	
	/*Choose Validity Period*/
	jQuery('#validity-inner').on("click", "a", function () {
		var $this = $(this);
	  	//console.log($(".selectPromo.inBlock.active h5").text());
	   	$this.siblings().removeClass('active');
	   	if ($this.hasClass('active')) {
	      	$this.removeClass('active')
			jQuery("#choosePromo, #commitment, #promoConfirm").hide(); 
			jQuery("#choosePromo a, #commitment a, .promoBox, .commitmentBox").removeClass('active');
			jQuery("p#promo").html("None")
			jQuery("p#commitmentBox").html("None")
			return false;
	   	} else {
	      	$this.addClass('active');
			jQuery("#choosePromo, #commitment, #promoConfirm").hide();
			jQuery("#choosePromo a, #commitment a").removeClass('active');
			var promo_id=jQuery("#promoType-inner .active").children('span').html();
			if(jQuery("#promoType-inner .active").children('h5').html()=='ALLNET TEXTS') name = 'ALLNET TEXTS'; else name = 'UNLI CALLS';
				var validity = jQuery("#validity-inner .active").children('h5').html();
				
				jQuery.getJSON(base_url+"getpromolist/"+promo_id+"/"+name+"/"+validity,{ajax:'true'},function(j){
				j.sort(sort_by('promoPrice', true, parseInt));
				var ht = '';
				for(var i=0;i<j.length;i++){
					if(j[i].promoTextDesc=="unli allnet texts" || j[i].promoTextDesc=="unlimited texts to all networks") var promoTextDesc = "Unli to all networks"; 
					else{
						var promoTextDesc = j[i].promoTextDesc;
					    promoTextDesc = promoTextDesc.replace(" to Globe/TM", "");
					}
					if(j[i].promoCallDesc=="unli calls to Globe/TM") var promoCallDesc = ConvertFirstCharacterToUpperCase(j[i].promoCallDesc);
					else{
						var promoCallDesc = j[i].promoCallDesc;
						if (/minutes/.test(promoCallDesc))
							promoCallDesc = promoCallDesc.replace(" of calls to Globe/TM", "") + "*";
						if (/mins of calls to Globe/.test(promoCallDesc))
							promoCallDesc = promoCallDesc.replace("mins of calls to Globe/TM", "") + "minutes*";
						if (/mins of Globe/.test(promoCallDesc))
							promoCallDesc = promoCallDesc.replace("mins of Globe/TM calls", "") + "minutes*";
						if (/null/.test(promoCallDesc))
							promoCallDesc = "None";				  
				    }
					var promoSurfDesc = j[i].promoSurfDesc;
					var promoPrice = j[i].promoPrice;
					promoSurfDesc = promoSurfDesc.replace(" data", "");
					promoPrice = promoPrice + "0";
					ht+='<a href="#." class="choosePromo"><span style="display:none;" class="'+j[i].promoPrice+'" id="promolistid" >'+j[i].promoId+'</span><ul><li><span class="choosePromoIcon one floatLeft"></span><span class=" floatRight width75fbselect">'+promoTextDesc+'</span><span class="clear marg0"></span></li><li><span class="choosePromoIcon two floatLeft"></span><span class=" floatRight width75fbselect margtopfbchoose3">'+promoCallDesc+'</span><span class="clear marg0"></span></li><li><span class="choosePromoIcon three floatLeft"></span><span class=" floatRight width75fbselect margtopfbchoose9">'+promoSurfDesc+'</span><span class="clear marg0"></span></li><li><span class="choosePromoIcon four floatLeft"></span><span class=" floatRight width75fbselect margtopfbchoose8">'+promoPrice+'</span><span class="clear marg0"></span></li></ul><h5>'+j[i].promoName+'</h5></a>';
				}
					jQuery("#choosePromo").show();
					jQuery("#choosePromo-inner").html(ht); 
					FB.Canvas.setSize({height: 800 });
					FB.Canvas.scrollTo(0,800);
					jQuery("body").css({"overflow":"hidden"});	
					jQuery("body").css({"overflow":"auto"});						
			}); 
	  	}
	    return false;
	});
	
	/*Choose Promo Details*/
	jQuery('#choosePromo-inner').on("click", "a", function () {
		var $this = $(this);
	   	$this.siblings().removeClass('active');
	   	if ($this.hasClass('active')) {
	      	$this.removeClass('active')
	     	jQuery("#commitment, #promoConfirm").hide();
			jQuery("#commitment a, .promoBox, .commitmentBox").removeClass('active');
			jQuery("p#promo, p#commitmentBox").html("None")
			return false;
	   	} else {
	      	$this.addClass('active');
			var $validity = jQuery("#validity-inner .active").children('h5').html() + " " + jQuery("#validity-inner .active").children('p').html();
			var $promo = $('a.choosePromo.active h5').text(); 
			var $txtdetails = $('a.choosePromo.active ul li:nth-child(1)').text();
			$txtdetails = ConvertFirstCharacterToUpperCase($txtdetails);
			var $calldetails = $('a.choosePromo.active ul li:nth-child(2)').text();
			var $surfdetails = $('a.choosePromo.active ul li:nth-child(3)').text();
			
			jQuery("#commitment, #promoConfirm").hide();
			jQuery(".promoBox").addClass("active");
			jQuery("#commitment a").removeClass('active');
			jQuery("p#promo").html($validity + ' - ' + $promo + '<br><br>' + $txtdetails + '<br>' + $calldetails+ '<br>' + $surfdetails );	
			
			var promo = $validity + ' - ' + $promo + '<br><br>' + $txtdetails + '<br>' + $calldetails + '<br>' + $surfdetails;
			document.cookie="fb_promo=" + promo + "; path=/";  
			var promolist_id=jQuery("#choosePromo-inner .active").children('span').html();
			document.cookie="fb_promoid=" + promolist_id + "; path=/";
			var price = jQuery("#choosePromo-inner .active").children('span').attr('class');
			promolist_id = '3';price = '3000.00';
			
			jQuery.getJSON(base_url+"getcommitmentperiod/"+promolist_id+"/"+price,{ajax:'true'},function(j){
				j.sort(sort_by('period', true, parseInt));
				var ht = '';
				for(var i=0;i<j.length;i++){
				   ht+='<a href="#." class="chooseCommited"><p class="floatLeft rb">Total savings: <b class="chooseCommitedpboldmod"> P'+j[i].totalRebate+'0 </b>Savings per registration:<b class="chooseCommitedpboldmod rba"> P'+j[i].rebateAmt+'0</b></p><h5 class="clear fontnormfb">'+j[i].period+' months</h5></a>';
				}
				jQuery("#commitment").show();
				jQuery("#commitment-inner").html(ht); 
				FB.Canvas.setSize({height: 1100 });
				FB.Canvas.scrollTo(0,1000);
				jQuery("body").css({"overflow":"hidden"});	
				jQuery("body").css({"overflow":"auto"});
			}); 
		}
	});
	
	/*Choose Rebate and Registration Period*/
	$('#commitment-inner').on("click", "a", function () {
		var $this = $(this);
		$this.siblings().removeClass('active');
		if ($this.hasClass('active')) {
			$this.removeClass('active')
			jQuery("#promoConfirm").hide();
			jQuery(".commitmentBox").removeClass("active");
			jQuery("p#commitmentBox").html("None")
			return false;
		} else {
			$this.addClass('active');
			jQuery("#promoConfirm").show();
			jQuery(".commitmentBox").addClass("active");
			
			var months=jQuery("#commitment-inner .active").children('h5').html();
			var totalrebate=jQuery("#commitment-inner .active .rb").children('b').html();
			var commitmentBox = months + ' - ' + totalrebate;
			document.cookie="fb_commitmentBox=" + commitmentBox + "; path=/";  
			document.cookie="fb_commitmentMonths=" + months.replace(" months", "") + "; path=/";
			jQuery("#commitmentBox").text(months + ' - ' + totalrebate);
		
			// Summary 
			var choosePromo = jQuery("#choosePromo-inner .active").children('h5').html();
			var txtdetails = jQuery('a.choosePromo.active ul li:nth-child(1)').text();
			var calldetails = jQuery('a.choosePromo.active ul li:nth-child(2)').text();
			var surfdetails = jQuery('a.choosePromo.active ul li:nth-child(3)').text();
			surfdetails = surfdetails.replace("surfing", "");
			var promodesc = txtdetails + ', ' + calldetails + ' and '+ surfdetails;
			var validity = jQuery("#validity-inner .active").children('h5').html() + " " + jQuery("#validity-inner .active").children('p').html();
			totalrebate = totalrebate.substring(2);
			//totalrebate = totalrebate.slice(0,-3);
			totalrebate = format2(parseInt(totalrebate), "P");
			//totalrebate = totalrebate.slice(0,-3);
			totalrebate = totalrebate.replace(/\s/g, "");
			rebateamt = jQuery("#commitment-inner .active .rb .rba").text();
			var price = jQuery("#choosePromo-inner .active").children('span').attr('class');
			price = format2(parseInt(price), "P");
			//price = price.slice(0,-3);
			price = price.replace(/\s/g, "");
		
			document.cookie="fb_choosePromo=" + choosePromo + "; path=/";
			document.cookie="fb_promodesc=" + promodesc + "; path=/";
			document.cookie="fb_validity=" + validity + "; path=/";
			document.cookie="fb_months=" + months + "; path=/";
			document.cookie="fb_totalrebate=" + totalrebate + "; path=/";
			document.cookie="fb_rebateamt=" + rebateamt + "; path=/";
			document.cookie="fb_promoprice=" + price + "; path=/";
		
			jQuery(".summary-promoname").text(choosePromo);
			jQuery("#summary-desc").text(promodesc);
			jQuery("#summary-validity").text(validity);
			jQuery("#summary-months").text(months);
			jQuery(".summary-totalrebate").text(totalrebate);
			jQuery("#summary-rebateamt").text(rebateamt);
			jQuery(".summary-promoprice").text(price);
			
			var gadgetprice = jQuery(".summary-gadgetprice").text();
			gadgetprice = gadgetprice.replace(",", "").substring(1);
			var promoprice = jQuery(".summary-promoprice").text();
			promoprice = promoprice.replace(",", "").substring(1);
			var totalcash = parseInt(gadgetprice) + parseInt(promoprice);
			totalcash = format2(parseInt(totalcash), "P");
			//totalcash = totalcash.slice(0,-3);
			totalcash = totalcash.replace(/\s/g, "");
			jQuery(".summary-totalcash").text(totalcash);
			document.cookie="fb_totalcash=" + totalcash + "; path=/";
		
			var totalrebate = jQuery(".summary-totalrebate").text();
			totalrebate = totalrebate.replace(",", "").substring(1);
			var discounted = parseInt(gadgetprice) - parseInt(totalrebate);
			discounted = format2(parseInt(discounted), "P");
			//discounted = discounted.slice(0,-3);
			discounted = discounted.replace(/\s/g, "");
			jQuery(".summary-discounted").text(discounted);	
			document.cookie="fb_discounted=" + discounted + "; path=/";
			FB.Canvas.setSize({height: 1500 });
			FB.Canvas.scrollTo(0,1500);
			jQuery("body").css({"overflow":"hidden"});	
			jQuery("body").css({"overflow":"auto"});
			return false;
		}
	});
	
	/*Process Button*/
	jQuery("#btn_ProceedOrder").click(function(){
		document.cookie="fb_current_pos=" + "3" + "; path=/";
	});
	
	if(/promo/.test(self.location.href)){
	   var mode = getCookie("fb_mode");
	   var promo = getCookieR("fb_promo");
	   var commitmentBox = getCookieR("fb_commitmentBox");
	   var mobtel = getCookieR("fb_mobtel");
	   var globestore = getCookieR("fb_globestore");
	   var username = getCookieR("fb_username");
	   var eadd = getCookieR("fb_eadd");
	   var bday = getCookieR("fb_bday");
	   var address = getCookieR("fb_address");
       if(mode=="edit"){
		 jQuery(".promoBox").addClass("active");
		 jQuery('#promo').html(promo);
		 jQuery(".commitmentBox").addClass("active");
		 jQuery('#commitmentBox').text(commitmentBox);
		 jQuery(".prepaidBox").addClass("active");
		 jQuery('#mobtel').text(mobtel);
	     jQuery(".pnone").hide();
	     jQuery(".profileBox").addClass("active");
		 jQuery('#name').text(username);
		 jQuery('#email').text(eadd);
		 jQuery('#dob').text(bday);
		 jQuery('#add').text(address);
		 jQuery("#pname").show();
		 jQuery("#pemail").show();
		 jQuery("#pdob").show();
		 jQuery("#padd").show();
	   	 jQuery(".storeBox").addClass("active");
		 jQuery('#globestore').text(globestore);
		 jQuery('#btn_ProceedOrder').text("Save");
       }
    }
});

   //Sort Array by Field Name
   var sort_by = function(field, reverse, primer){

   var key = primer ? 
       function(x) {return primer(x[field])} : 
       function(x) {return x[field]};

   reverse = [-1, 1][+!!reverse];

   return function (a, b) {
       return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
     } 
   }
   
   //Format Price
   function format2(n, currency) {
    return currency + " " + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
   }
   
   //ConvertFirstCharacterToUpperCase
   function ConvertFirstCharacterToUpperCase(text) {
    return text.substr(0, 1).toUpperCase() + text.substr(1);    
   }
   
   //Retrieve Cookie
   function getCookie(fb_cname) {
	var name = fb_cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) 
	{
	var c = ca[i].replace(/\s/g, "");
	if (c.indexOf(name)==0) return c.substring(name.length,c.length);
	}
	return "";
   }
   
   //Retrieve Cookie
   function getCookieR(fb_name) {
    var nameEQ = fb_name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
   }
  

/* . . . . . . . . . . . . . . . Ajax Services . . . . . . . . . . . . . . . . . . */

function ajaxService(/* ... */){ 
	
	ajaxUrl = baseurl + "promo/serviceCalls";
	
	// argument[0] is always promo call type 
    if(arguments[0] == "validityPeriod"){
    	//prepare post data  arguments[0] = validityPeriod
		$.post(ajaxUrl, { isAjax:"true", method:arguments[0], promoTypeId :arguments[1]   }, function( data,status ) {
	  	  if(status == "success"){
	  	  		// alert(data);   // . . . . . . practice 
	  	 		console.log(".  .  .  .  .  .");	  	 	
	  	 		
	  	  } 
    	});
    }
    if(arguments[0] == "getPromoList"){
		$.post(ajaxUrl, { isAjax:"true", method:arguments[0], get:"All"  }, function( data,status ) {
	  	  if(status == "success"){
	  	 	
	  	  		
	  	  } 
    	});
    }
    if(arguments[0] == "getCommitmentPeriod"){
		$.post(ajaxUrl, { isAjax:"true", method:arguments[0], get:"All"  }, function( data,status ) {
	  	  if(status == "success"){
	  	 	  	  		
	  	  } 
    	});
    }
}

 /*
 * &  &  &  &  &  &  &  &  &  &  &
 * . . . . . . . . . . . 
 * .  
 * . 1.  getPromoTypeList on load
 * . 2.  getPromoValidityList when promo type clicked
 * . 3.  getPromoList when days clicked
 * . 4.  getCommitmentPeriod when promo clicked
 * .
 * .
 * . . . . . . . . . . .
 * 
 */ 

